

#include<bits/stdc++.h>
using namespace std;



class Solution 
{
    public:
    //Function to find if there is a celebrity in the party or not.
    int celebrity(vector<vector<int> >& M, int n) 
    {
        stack <int> s;
        for(int i=0;i<n;i++)
            s.push(i);
        int a,b;
        while(s.size()>1)
        {
            a=s.top();
            s.pop();
            b=s.top();
            s.pop();
            if(M[a][b]==1)
                s.push(b);
            else
                s.push(a);
        }
        int result= s.top(); 
        for(int i=0;i<n;i++)
        {
            if(i!=result)
            {
                if(M[result][i] ==1 || M[i][result]==0 )
                    return -1;
            }
        }
        return result;
    }
};

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<vector<int> > M( n , vector<int> (n, 0));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                cin>>M[i][j];
            }
        }
        Solution ob;
        cout<<ob.celebrity(M,n)<<endl;

    }
}

